home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Interface Classes / CPPPictButton.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  8.2 KB  |  334 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/23/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPPictButton
  6.     
  7.     SUPERCLASS: CPPVisualObject
  8.     
  9.         This C++ class manages a button which is drawn with pictures
  10.         rather than a control definition.  
  11.     
  12. ********************************************************************/
  13.  
  14. #include <CPPPictButton.h>
  15. #include <CPPWindow.h>
  16.  
  17. extern Rect kDefaultRect;
  18. extern Rect kEmptyRect;
  19. extern RGBColor    RGBBlack;
  20. extern RGBColor    RGBWhite;
  21.  
  22. /*-----------------------------------------------------------------*/
  23. /*------------------------ PUBLIC METHODS -------------------------*/
  24. /*-----------------------------------------------------------------*/
  25.  
  26.     CPPPictButton::CPPPictButton (CPPWindow *itsWindow, short upPictID,
  27.                                  short downPictID, Point topLeft,
  28.                                  Boolean isFramed,
  29.                                  Boolean canBeTarget,
  30.                                  Boolean active, Boolean visible) :
  31.                 CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget, 
  32.                                  active, visible)
  33.     /* initialize the picture button to work by alternating two pictures */
  34.     {
  35.         if (this->owningWindow)
  36.           {
  37.             if ((this->upPict = GetPicture (upPictID)) != NULL)
  38.               {
  39.                 DetachResource ((Handle)this->upPict);
  40.                 this->bounds = (**upPict).picFrame;
  41.                 OffsetRect (&this->bounds, topLeft.h - this->bounds.left,
  42.                                        topLeft.v - this->bounds.top);
  43.               }
  44.             
  45.             if ((this->downPict = GetPicture (downPictID)) != NULL)
  46.               DetachResource ((Handle)this->downPict);
  47.           }
  48.           
  49.         this->hasFrame = isFramed;
  50.         this->isEnabled = TRUE;
  51.         this->hiliteType = kUsePicture;
  52.     }
  53.  
  54. /*-----------------------------------------------------------------*/
  55.  
  56.     CPPPictButton::CPPPictButton (CPPWindow *itsWindow, short thePictID,
  57.                                  Point topLeft, short hiliteMethod,
  58.                                  Boolean isFramed,
  59.                                  Boolean useWindowFont,
  60.                                  Boolean canBeTarget,
  61.                                  Boolean active, Boolean visible) :
  62.                 CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget, 
  63.                                  active, visible)
  64.     {                
  65.         if (this->owningWindow)
  66.           {
  67.             if ((this->upPict = GetPicture (thePictID)) != NULL)
  68.               {
  69.                 DetachResource ((Handle)this->upPict);
  70.                 this->bounds = (**upPict).picFrame;
  71.                 OffsetRect (&this->bounds, topLeft.h - this->bounds.left,
  72.                                        topLeft.v - this->bounds.top);
  73.               }
  74.             this->downPict = NULL;
  75.           }
  76.           
  77.         this->hasFrame = isFramed;
  78.         this->isEnabled = TRUE;
  79.         this->hiliteType = hiliteMethod;
  80.     }
  81.  
  82. /*-----------------------------------------------------------------*/
  83.  
  84.     CPPPictButton::~CPPPictButton (void)
  85.     {
  86.         if (this->upPict)
  87.           DisposeHandle ((Handle)this->upPict);
  88.         if (this->upPict)
  89.           DisposeHandle ((Handle)this->downPict);
  90.     }
  91.  
  92. /*-----------------------------------------------------------------*/
  93.  
  94.     char    *CPPPictButton::ClassName (void)
  95.     {
  96.         return "CPPPictButton";
  97.     }
  98.  
  99. /*-----------------------------------------------------------------*/
  100.  
  101.     Boolean    CPPPictButton::DoClick (EventRecord *theEvent)
  102.     /* handle a click in the picture button - if visible */
  103.     {
  104.         Point    myPt = theEvent->where;
  105.         
  106.         if (this->IsVisible() && this->isEnabled)
  107.           {
  108.                Global2Local (&myPt);
  109.                if (TrackPictButton(myPt))
  110.                  DoOnClick ();
  111.           }
  112.         
  113.         return FALSE;
  114.     }
  115.  
  116. /*-----------------------------------------------------------------*/
  117.  
  118.     void    CPPPictButton::Activate (Boolean nowActive)
  119.     /* activate/deactivate our control */
  120.     /* IS OVERRIDE */
  121.     {
  122.         // buttons do not visually change when in background or foreground
  123.         
  124.         CPPVisualObject::Activate(nowActive);
  125.     }
  126.  
  127. /*-----------------------------------------------------------------*/
  128.  
  129.     void    CPPPictButton::MakeVisible (Boolean nowVisible)
  130.     /* hide/show our button */
  131.     /* IS OVERRIDE */
  132.     {
  133.         
  134.           if (!nowVisible)
  135.             EraseRect (GetBounds());
  136.           InvalRect (GetBounds());
  137.       
  138.         CPPVisualObject::MakeVisible(nowVisible);
  139.     }
  140.  
  141. /*-----------------------------------------------------------------*/
  142.  
  143.     void    CPPPictButton::MoveContent (short newH, short newV)
  144.     /* move the picture button to a new position */
  145.     {
  146.         OffsetRect (&this->bounds, newH - this->bounds.left,
  147.                                    newV - this->bounds.top);
  148.     }
  149.  
  150. /*-----------------------------------------------------------------*/
  151.  
  152.     Rect    *CPPPictButton::GetBounds (void)
  153.     /* return the boundsrect of the object */
  154.     /* SUBCLASS MUST OVERRIDE */
  155.     {
  156.         if (this->isVisible)
  157.           return &this->bounds;
  158.         else
  159.           return &kEmptyRect;
  160.     }
  161.  
  162. /*-----------------------------------------------------------------*/
  163.  
  164.     void    CPPPictButton::Draw ()
  165.     /* is override */
  166.     {
  167.         PenState    OldState;
  168.         Rect        frameRect = *GetBounds();
  169.         RGBColor    MaskGray    =     {20000, 20000, 20000};
  170.  
  171.         if (this->IsVisible())
  172.           {
  173.               GetPenState(&OldState);
  174.         
  175.             // draw the button in it's normal state
  176.               if (this->upPict)
  177.                 DrawPicture (this->upPict, &frameRect);
  178.               
  179.               // disable it, if necessary
  180.               if (!this->isEnabled)
  181.                 {
  182.                   if (InColorWindow())
  183.                     {
  184.                         OpColor (&RGBWhite);
  185.                     PenMode (addPin);
  186.                     RGBForeColor(&MaskGray);
  187.                      PaintRect(&frameRect);
  188.                       RGBForeColor(&RGBBlack);
  189.                      PenMode (srcCopy);
  190.                     }
  191.                   else
  192.                     {
  193.                         PenPat(gray);
  194.                     PenMode(patBic);
  195.                     PaintRect(&frameRect);
  196.                     }
  197.                 }
  198.               
  199.               // draw a frame around it, if necessary
  200.               if (this->hasFrame)
  201.                 {
  202.                     InsetRect(&frameRect, -4, -4);
  203.                     PenMode(patCopy);
  204.                     PenPat(black);
  205.                     PenSize (3,3);
  206.                     FrameRect(&frameRect);
  207.                 }
  208.               
  209.               SetPenState(&OldState);
  210.           }
  211.     }
  212.  
  213. /*-----------------------------------------------------------------*/
  214.  
  215.     void    CPPPictButton::ResizeContent (short newWidth, short newHeight)
  216.     /* change the size of the visible list */
  217.     {
  218.         this->bounds.right = this->bounds.left+newWidth;
  219.         this->bounds.bottom = this->bounds.top+newHeight;
  220.         
  221.     }
  222.     
  223. /*-----------------------------------------------------------------*/
  224.  
  225.     void    CPPPictButton::DoOnClick (void)
  226.     /* perform some action when the picture button is clicked */
  227.     /* SUBCLASS SHOULD OVERRIDE */
  228.     {
  229.         if (this->doClickProc)    // call the user-specified click handler
  230.          (doClickProc)(this->owningWObject);
  231.         else
  232.         if (this->commandEquivalent != kNoCommand)
  233.           this->owningWObject->DoCommand (this->commandEquivalent);
  234.     }
  235.  
  236. /*-----------------------------------------------------------------*/
  237.  
  238.     void    CPPPictButton::EnableButton (Boolean nowEnabled)
  239.     {
  240.           if (nowEnabled != this->isEnabled)
  241.             {
  242.               InvalRect (GetBounds());
  243.               this->isEnabled = nowEnabled;
  244.             }
  245.     }
  246.  
  247. /*-----------------------------------------------------------------*/
  248.  
  249.     void    CPPPictButton::HilightButton (Boolean doHilight)
  250.     {
  251.         RGBColor    MaskGray    =     {20000, 20000, 20000};
  252.         Rect    tempRect = *GetBounds();
  253.         PenState    OldState;
  254.         
  255.         GetPenState (&OldState);
  256.         
  257.         if (doHilight)
  258.           {
  259.               switch (this->hiliteType) {
  260.                 case kUsePicture :
  261.                     if (this->downPict)
  262.                       DrawPicture (this->downPict, &tempRect);
  263.                     break;
  264.                     
  265.                 case kUseInvert :
  266.                     InvertRect (&tempRect);
  267.                     break;
  268.                     
  269.                 case kUseDarken :
  270.                 if (InColorWindow())
  271.                   {
  272.                         OpColor (&RGBBlack);
  273.                     PenMode (subPin);
  274.                     RGBForeColor(&MaskGray);
  275.                      PaintRect(&tempRect);
  276.                       RGBForeColor(&RGBBlack);
  277.                      PenMode (srcCopy);
  278.                   }
  279.                 else
  280.                   FillRect (&tempRect, black);
  281.                     break;
  282.               }
  283.           }
  284.         else
  285.           {
  286.               if (this->upPict)
  287.                 DrawPicture (this->upPict, &tempRect);
  288.           }
  289.     
  290.         SetPenState (&OldState);
  291.  
  292.     }
  293.  
  294. /*-----------------------------------------------------------------*/
  295. /*----------------------- PRIVATE METHODS -------------------------*/
  296. /*-----------------------------------------------------------------*/
  297.  
  298.     Boolean    CPPPictButton::TrackPictButton (Point thePoint)
  299.     /* given 'thePoint' in local coordinates, simulate clicking */
  300.     /* on the picture button until the user releases the mouse button */
  301.     {
  302.         Boolean    isHilighted = TRUE;
  303.         Point    newMouse;
  304.         Rect    tempRect = *GetBounds();
  305.         
  306.         if (PtInRect (thePoint, &tempRect))
  307.           {
  308.               HilightButton (isHilighted);
  309.               while (StillDown())
  310.                 {
  311.                     GetMouse (&newMouse);
  312.                     if (PtInRect(newMouse, &tempRect))
  313.                       {
  314.                           if (!isHilighted)
  315.                             HilightButton(isHilighted = TRUE);
  316.                       }
  317.                     else
  318.                       {
  319.                           if (isHilighted)
  320.                             HilightButton (isHilighted = FALSE);
  321.                       }
  322.                 }
  323.                 
  324.               if (isHilighted)
  325.                 HilightButton (FALSE);
  326.               return isHilighted;
  327.               
  328.           }
  329.           
  330.         return FALSE;
  331.     }
  332.  
  333.  
  334.